<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>节流throttle</title>
</head>
<body>
<ul>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
  <li>1</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
  <li>6</li>
  <li>7</li>
  <li>8</li>
  <li>9</li>
</ul>
</body>
<style>
  body{
    display: flex;
    justify-content: center;
  }
  ul, li {
    padding:0;
    margin: 0;
  }
  li{
    width: 250px;
    height:200px;
    list-style: none;
    display: flex;
    justify-content: center;
    align-items: center;
  }
  li:nth-child(2n){
    background:whitesmoke;
  }
  li:nth-child(2n+1){
    background:yellow;
  }
</style>
<script>
  // 首节流,时间戳的实现，停止触发后，没办法再次执行

  function throttle(fn, interval){
    let prev = 0;
    return function(...args){
      const now = Date.now();
      if( now-prev >= interval ){
        prev = now
        fn.apply(this, args)
      }
    }
  }
  function handle(){
    console.log(new Date())
  }

  const throttleHandler = throttle(handle, 3000)
  console.log('tt', throttleHandler)
  window.addEventListener('scroll', throttleHandler)
</script>
</html>
